package com.example.dishit.view.friendmaint;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.dishit.R;
import com.example.dishit.controller.friendmaint.FriendMaintController;
import com.example.dishit.controller.main.MainController;
import com.example.dishit.model.dbproxy.DBProxyFactory;
import com.example.dishit.model.dbproxy.IDBProxyCallback;
import com.example.dishit.model.dish.DishData;

public class FriendMaintActivity extends ListActivity implements IFriendMaintView {
    
    private ArrayAdapter<String> listAdapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Allow multi-selection on the list.
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        getListView().setSelector(R.drawable.background);
        
        // Create "Friend Maintenance" controller.
        FriendMaintController.instance().setView(this);
        
        // Initialize the List Adapter.
        listAdapter = new FriendArrayAdapter(this, new ArrayList<String>());
        setListAdapter(listAdapter);
        
        // Locate my Dishers and put them in the list.
        final ListActivity thisListActivity = this;
        DBProxyFactory.instance().getMyDishers(MainController.THIS_USER_ID, new IDBProxyCallback() {
            public void onResultCallback(boolean wasSuccessful, String message) {}
            public void onDishDataCallback(List<DishData> dishDatas) {}
            public void onDishersCallback(final List<String> myDishers) {
                thisListActivity.runOnUiThread(new Runnable() {
                    public void run() {
                        listAdapter.addAll(myDishers);
                    }
                });
            }
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_friend_maint, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.follow_button:
                showDialog();
                return true;
            case R.id.unfollow_button:
                final Activity thisActivity = this;
                final List<String> selectedUsers = FriendMaintController.instance().getSelectionsSnapshot();
                for(final String user : selectedUsers) {
                    DBProxyFactory.instance().unfollowUser(MainController.THIS_USER_ID, user, new IDBProxyCallback() {
                        public void onResultCallback(boolean wasSuccessful, String message) {
                            // Make sure to indicate that user is no longer selected.
                            if(wasSuccessful) FriendMaintController.instance().removeSelection(user);
                            
                            // Update the View.
                            thisActivity.runOnUiThread(new Runnable() {
                                public void run() {
                                    listAdapter.remove(user);
                                }
                            });
                        }
                        public void onDishersCallback(List<String> myDishers) {}
                        public void onDishDataCallback(List<DishData> dishDatas) {}
                    });
                }
                return true;
            case R.id.done_friend_maint_button:
                FriendMaintController.instance().cancel();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    private void showDialog() {
        // Create the fragment and show it as a dialog.
        final FriendMaintDialogFragment newFragment = new FriendMaintDialogFragment();
        newFragment.setArrayAdapter(listAdapter);
        newFragment.show(getFragmentManager(), "dialog");
    }
    
    protected void onStop() {
        super.onStop();
        FriendMaintController.instance().clearSelections();
    }
}
